//
//  BCStuffInfoViewController.m
//  BangcaiBuy
//
//  Created by lsy on 16/6/13.
//  Copyright © 2016年 BCB. All rights reserved.
//

#import "BCStuffInfoViewController.h"
#import "BCBannerCell.h"
#import "BCStuffInfoInteractiveCollectionViewCell.h"
#import "BCStuffInfoDiscriptionCollectionViewCell.h"
#import "BCStuffInfoBannerCollectionViewCell.h"
#import "BCOrderViewController.h"

@interface BCStuffInfoViewController () <UICollectionViewDelegate,UICollectionViewDataSource,BCStuffInfoBannerCellDelegate,shopCarDelegate>
@property (nonatomic, strong) UICollectionView *mainCollectionView;
@property (nonatomic,strong) CALayer *dotLayer;
@property (nonatomic,assign) CGFloat endPointX;
@property (nonatomic,assign) CGFloat endPointY;
@property (nonatomic,strong) UIBezierPath *path;
@end

@implementation BCStuffInfoViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    [self p_initNavgationBar];
    [self p_initCollectionView];
    //分类小红点
    _endPointX = SCREEN_WIDTH*0.06f;
    _endPointY = SCREEN_HEIGHT*0.95f;
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self p_initUnderBar];
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = YES;
    [[BCShopCarManager sharedInstance] layOutWithLogo];
    [BCShopCarManager sharedInstance].butTitle = @"下订单";

    [BCShopCarManager sharedInstance].delegate = self;
    
    if (!self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
    
    [_mainCollectionView reloadData];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    if (self.rdv_tabBarController.tabBarHidden) {
        [[self rdv_tabBarController] setTabBarHidden:NO animated:YES];
    }
}

- (void)logoTo
{
    BCOrderViewController *secondViewController = [[BCOrderViewController alloc] init];
    [self.navigationController pushViewController:secondViewController animated:YES];
    
}

- (void)goTo
{
    if (self.popOrPush) {
         [self.navigationController popViewControllerAnimated:YES];
        self.popOrPush = NO;
    } else{
        BCOrderViewController *secondViewController = [[BCOrderViewController alloc] init];
        [self.navigationController pushViewController:secondViewController animated:YES];
    }
}

#pragma mark - View init
-(void)p_initNavgationBar {
    self.title = @"商品详情";
    self.view.backgroundColor = [UIColor colorWithRed:242.f/255.f green:242.f/255.f blue:242.f/255.f alpha:1];
}

-(void)p_initUnderBar {

    [[BCShopCarManager sharedInstance] showShopWindow];
}

- (void)p_initCollectionView {
    UICollectionViewFlowLayout *flowLayout= [[UICollectionViewFlowLayout alloc]init];
    self.mainCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, -20.f, SCREEN_WIDTH, SCREEN_HEIGHT) collectionViewLayout:flowLayout];
    self.mainCollectionView.delegate = self;
    self.mainCollectionView.dataSource = self;
    //设置contentInset属性（上左下右的值）
    self.mainCollectionView.contentInset = UIEdgeInsetsMake(0, 0, 12.f, 0);
    self.mainCollectionView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.mainCollectionView];
}
#pragma mark - Private Action


#pragma mark - UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 2;
    }
    else {
        return 1;
    }
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *dataArray = [BCShopViewManager readDataFromStuff:_stuff_uuid];
    STUFF *model = dataArray.lastObject;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            static NSString *ID = @"StuffInfoBannerCell";
            BCStuffInfoBannerCollectionViewCell *cell = [BCStuffInfoBannerCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            cell.imgName = model.icon;
            cell.delegate = self;
            __weak typeof(self) weakSelf = self;
            cell.Back = ^(){
                [weakSelf.navigationController popViewControllerAnimated:YES];
            };
            [cell p_initScrollView];
            return cell;
        }
        else {
            static NSString *ID = @"StuffInfoInteractiveCell";
            BCStuffInfoInteractiveCollectionViewCell *cell = [BCStuffInfoInteractiveCollectionViewCell cellWithCollectionView:collectionView];
            cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
            ORDER *order_model = [BCOrderViewManager readStuffFromOrder:_stuff_uuid];
            NSNumber *count = @(0);
            if (order_model) {
                count = order_model.count;
            }
            cell.stuffNameLable.text = model.name;
            cell.priceLable.text = [NSString stringWithFormat:@"￥%@",[model.price rounding:2]];
            cell.perLable.text = [NSString stringWithFormat:@"规格 %@",model.unit];
            cell.countTextFeild.text = count.stringValue;
            cell.uuid = model.uuid;
            [cell makeAttributedString];
            __weak __typeof(&*cell)weakCell =cell;
            cell.reddot = ^() {
                CGRect parentRect = [weakCell convertRect:weakCell.addButton.frame toView:self.view];
                [self JoinCartAnimationWithRect:parentRect];
            };
            return cell;
        }
    }
    else {
        static NSString *ID = @"StuffInfoDiscriptionCell";
        BCStuffInfoDiscriptionCollectionViewCell *cell = [BCStuffInfoDiscriptionCollectionViewCell cellWithCollectionView:collectionView];
        cell = [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
        cell.discriptionLable.text = model.discribtion;
        return cell;
    }
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    if (section == 0) {
        UIEdgeInsets top = {0,0,0,0};
        return top;
    }
    else {
        UIEdgeInsets top = {5.f,0,0,0};
        return top;
    }
}

//定义每个UICollectionViewCell 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGFloat adjustScreen_Height = SCREEN_HEIGHT-49.f;
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            if (SCREEN_WIDTH <=320) {
                return CGSizeMake(SCREEN_WIDTH, adjustScreen_Height*0.63f-25.f);
            }
            else {
                return CGSizeMake(SCREEN_WIDTH, adjustScreen_Height*0.63f);
            }
        }
        else {
            if (SCREEN_WIDTH <=320) {
                return CGSizeMake(SCREEN_WIDTH, adjustScreen_Height*0.21f+25.f);
            }
            else {
                return CGSizeMake(SCREEN_WIDTH, adjustScreen_Height*0.21f);
            }
        }
    }
    else {
        return CGSizeMake(SCREEN_WIDTH, adjustScreen_Height*0.16f);
    }
}

//每个item之间的间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    else {
        return 0;
    }
}

//每个section中不同的行之间的行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    else {
        return 0;
    }
}

//collectionview点击
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"cell %@,%@ selected",@(indexPath.section),@(indexPath.row));
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            
        }
        else {
        }
    }
    else {
    }
}

#pragma mark - LJBannerCellDelegate
- (void)bannerCellClick:(BCBannerCell *)forumTitleView andWithBtnTag:(NSInteger)tag {
    
}

#pragma mark -加入购物车动画
-(void) JoinCartAnimationWithRect:(CGRect)rect
{
    
    CGFloat startX = rect.origin.x;
    CGFloat startY = rect.origin.y;
    
    _path= [UIBezierPath bezierPath];
    [_path moveToPoint:CGPointMake(startX, startY)];
    //三点曲线
    [_path addCurveToPoint:CGPointMake(_endPointX, _endPointY)
             controlPoint1:CGPointMake(startX, startY)
             controlPoint2:CGPointMake(startX - 180, startY - 200)];
    
    _dotLayer = [CALayer layer];
    _dotLayer.backgroundColor = [UIColor redColor].CGColor;
    _dotLayer.frame = CGRectMake(0, 0, 15, 15);
    _dotLayer.cornerRadius = (15 + 15) /4;
    [self.view.layer addSublayer:_dotLayer];
    [self groupAnimation];
    
}
#pragma mark - 组合动画
-(void)groupAnimation
{
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    animation.path = _path.CGPath;
    animation.rotationMode = kCAAnimationRotateAuto;
    
    CABasicAnimation *alphaAnimation = [CABasicAnimation animationWithKeyPath:@"alpha"];
    alphaAnimation.duration = RedAnimationDuring;
    alphaAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    alphaAnimation.toValue = [NSNumber numberWithFloat:0.1];
    alphaAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    
    CAAnimationGroup *groups = [CAAnimationGroup animation];
    groups.animations = @[animation,alphaAnimation];
    groups.duration = RedAnimationDuring+0.3f;
    groups.removedOnCompletion = NO;
    groups.fillMode = kCAFillModeForwards;
    groups.delegate = self;
    [groups setValue:@"groupsAnimation" forKey:@"animationName"];
    [_dotLayer addAnimation:groups forKey:nil];
    
    [self performSelector:@selector(removeFromLayer:) withObject:_dotLayer afterDelay:RedAnimationDuring+0.3f];
    
}
- (void)removeFromLayer:(CALayer *)layerAnimation{
    
    [layerAnimation removeFromSuperlayer];
}

#pragma mark - CAAnimationDelegate
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    CABasicAnimation *shakeAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    shakeAnimation.duration = 0.25f;
    shakeAnimation.fromValue = [NSNumber numberWithFloat:0.9];
    shakeAnimation.toValue = [NSNumber numberWithFloat:1];
    shakeAnimation.autoreverses = YES;
    [[BCShopCarManager sharedInstance].logoImg.layer addAnimation:shakeAnimation forKey:nil];
}


@end
